package com.press.controllers.blog;

import com.press.bean.ContentBean;
import com.press.bean.TaxonomyBean;
import com.press.beanforview.VContentBean;
import com.press.beanforview.VPageBean;
import com.press.controllers.admin.PanelController;
import com.press.services.SearchService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by haocongshun on 2016/10/2.
 */
@Controller
@RequestMapping("/blog")
public class SearchController {
    private static int pageNow = 1;
    private static int pageCount;
    @Resource(name = "searchService")
    private SearchService searchService;
    private static Logger log = Logger.getLogger(PanelController.class);


    /**
     * 根据搜索关键词searchcontent搜索文章的概要
     * 返回搜索页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/search")
    public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        log.info("*****************进入search");
        UtilController utilController = (UtilController) WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()).getBean("utilController");
        utilController.contentsRecommend(request);
        utilController.contentsTop10(request);
        utilController.menus(request);

        String search=request.getParameter("search");

        int searchcount=searchService.getcountforsearch(search);
        pageCount =searchcount/6+1;
        handlePage(request);



        if(search!=null){
            List<ContentBean> contentresult  = searchService.searchfromsummarypage(search,String.valueOf(pageNow));
            List<TaxonomyBean> taxonomys = new ArrayList<>();
            for (int i=0; i<contentresult.size();i++){
                List<TaxonomyBean> tbs=searchService.gettaxonomybycontent_id(contentresult.get(i).getContent_id());
                for(TaxonomyBean tb:tbs){
                    taxonomys.add(tb);
                }
            }
            List<VContentBean> contentsSearch=searchService.set_cb_tb_invb(contentresult,taxonomys,request.getContextPath());

            request.setAttribute("taxonomys",taxonomys);
            request.setAttribute("contentsSearch",contentsSearch );

            log.info("*****************search变量赋值完成");
        }else{
            request.setAttribute("search","未输入关键词");
            log.info("*****************search未输入关键词");
        }

        getPage(request,search);
        request.setAttribute("search",search);
        Map<String,String> option = (Map<String ,String>)request.getServletContext().getAttribute("Option");
        String forwardUrl = "/"+option.get("web_template_id")+"/search.jsp";
        request.getRequestDispatcher(forwardUrl).forward(request,response);

    }

    public void handlePage(HttpServletRequest request){
        String pn = request.getParameter("pn");
        if(pn==null){
            pageNow=1;
        }else if(pn.equals("next")){
            if(pageNow<pageCount){
                pageNow++;
            }
        }else if(pn.equals("last")){
            if(pageNow>1){
                pageNow--;
            }
        }else {
            pageNow=Integer.parseInt(pn);
        }
    }

    private void getPage(HttpServletRequest request,String search) throws UnsupportedEncodingException {

        List<VPageBean> vPages = new ArrayList<>();

        String urlPage = request.getServletContext().getAttribute("URLSearch")+"?search="+ URLDecoder.decode(search,"utf-8")+"&pn=";

        VPageBean last = new VPageBean();
        last.setStatus("");
        last.setNum("<<");
        last.setUrl(urlPage+"last");
        vPages.add(last);

        for(int i = 1;i<=pageCount;i++){
            VPageBean vPage = new VPageBean();
            vPage.setNum(String.valueOf(i));
            vPage.setUrl(urlPage+i);
            if(i==pageNow){
                vPage.setStatus("active");
            }else {
                vPage.setStatus("");
            }
            vPages.add(vPage);
        }

        VPageBean next = new VPageBean();
        next.setStatus("");
        next.setNum(">>");
        next.setUrl(urlPage+"next");
        vPages.add(next);

        request.setAttribute("pages",vPages);

        request.setAttribute("pageCount",pageCount);

        request.setAttribute("pageNow",pageNow);
    }
    public SearchService getSearchService() {
        return searchService;
    }

    public void setSearchService(SearchService searchService) {
        this.searchService = searchService;
    }
}
